home *** CD-ROM | disk | FTP | other *** search
- #include <ctype.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <newt.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/mount.h>
- #include <unistd.h>
-
- #include "hd.h"
- #include "fs.h"
- #include "install.h"
- #include "log.h"
- #include "methods.h"
- #include "net.h"
- #include "windows.h"
-
- /* This was split into two pieces to keep the initial install program small */
-
- static int nfsPrepare(struct installMethod * method);
- static int smbPrepare(struct installMethod * method);
- static int cdromPrepare(struct installMethod * method);
- int floppyRoot(struct installMethod * method);
- static int nfsGetSetup(char ** hostptr, char ** dirptr);
- static int smbGetSetup(char ** hostptr, char ** dirptr, char ** acctptr,
- char ** pwptr);
- static int totalMemory(void); /* in K */
- static int smbPrepare(struct installMethod * method);
-
- static struct installMethod methods[] = {
- { "Local CDROM", "cdrom", 0, cdromPrepare, NULL, NULL,
- NULL, NULL, NULL },
- { "NFS image", "nfs", 0, nfsPrepare, NULL, NULL,
- NULL, NULL, NULL },
- { "hard drive", "hd", 0, floppyRoot, NULL, NULL,
- NULL, NULL, NULL },
- { "FTP", "ftp", 0, floppyRoot, NULL, NULL,
- NULL, NULL, NULL },
- #ifdef __i386__
- { "SMB image", "smb", 0, floppyRoot, NULL, NULL,
- NULL, NULL, NULL },
- #endif
- #if 0
- { "SCSI Tape Install", "tape", 0, floppyRoot, NULL, NULL,
- NULL, NULL, NULL },
- #endif
- } ;
- static int numMethods = sizeof(methods) / sizeof(struct installMethod);
-
- #define LOAD_BLOCK_COUNT 16
- static int loadRamdisk(char * todev, char * fromdev, int blocks,
- char * label) {
- newtComponent form, scale;
- char * topath, * frompath;
- char buf[LOAD_BLOCK_COUNT * 1024];
- int rc = 0;
- int i;
- int to, from;
-
- if (blocks % LOAD_BLOCK_COUNT) {
- logMessage("internal error: blocks in loadRamdisk() must be "
- "divisible by %d!!", LOAD_BLOCK_COUNT);
- return 1;
- }
-
- topath = alloca(strlen(todev) + 8);
- sprintf(topath, "/tmp/%s", todev);
-
- frompath = alloca(strlen(fromdev) + 8);
- sprintf(frompath, "/tmp/%s", fromdev);
-
- if (devMakeInode(todev, topath)) return 1;
- if (devMakeInode(fromdev, frompath)) {
- unlink(topath);
- return 1;
- }
-
- to = open(topath, O_WRONLY);
- if (to < 0) {
- logMessage("failed to open %s: %s", topath, strerror(errno));
- unlink(topath);
- unlink(frompath);
- return 1;
- }
-
- from = open(frompath, O_RDONLY);
- if (from < 0) {
- logMessage("failed to open %s: %s", frompath, strerror(errno));
- unlink(topath);
- unlink(frompath);
- return 1;
- }
-
- unlink(frompath);
- unlink(topath);
-
- logMessage("copying %d blocks from %s to %s", blocks, fromdev, todev);
-
- newtOpenWindow(10, 10, 60, 5, "Loading");
-
- form = newtForm(NULL, NULL, 0);
-
- newtFormAddComponent(form, newtLabel(1, 1, label));
- scale = newtScale(1, 3, 58, blocks / LOAD_BLOCK_COUNT);
- newtFormAddComponent(form, scale);
- newtDrawForm(form);
- newtRefresh();
-
- for (i = 0; i < (blocks / LOAD_BLOCK_COUNT) && !rc; i++) {
- newtScaleSet(scale, i);
- newtRefresh();
-
- if (read(from, buf, sizeof(buf)) != sizeof(buf)) {
- logMessage("error reading from device: %s", strerror(errno));
- rc = 1;
- } else {
- if (write(to, buf, sizeof(buf)) != sizeof(buf)) {
- logMessage("error writing to device: %s", strerror(errno));
- rc = 1;
- }
- }
- }
-
- newtPopWindow();
- newtFormDestroy(form);
-
- close(from);
- close(to);
-
- return rc;
- }
-
- static int totalMemory(void) {
- int fd;
- int bytesRead;
- char buf[4096];
- char * chptr, * start;
- int total = 0;
-
- fd = open("/proc/meminfo", O_RDONLY);
- if (fd < 0) {
- logMessage("failed to open /proc/meminfo: %s", strerror(errno));
- return 0;
- }
-
- bytesRead = read(fd, buf, sizeof(buf) - 1);
- if (bytesRead < 0) {
- logMessage("failed to read from /proc/meminfo: %s", strerror(errno));
- close(fd);
- return 0;
- }
-
- close(fd);
- buf[bytesRead] = '\0';
-
- chptr = buf;
- while (*chptr && !total) {
- if (*chptr != '\n' || strncmp(chptr + 1, "MemTotal:", 9)) {
- chptr++;
- continue;
- }
-
- start = ++chptr ;
- while (*chptr && *chptr != '\n') chptr++;
-
- *chptr = '\0';
-
- logMessage("found total memory tag: \"%s\"", start);
-
- while (!isdigit(*start) && *start) start++;
- if (!*start) {
- logMessage("no number appears after MemTotal tag");
- return 0;
- }
-
- chptr = start;
- while (*chptr && isdigit(*chptr)) {
- total = (total * 10) + (*chptr - '0');
- chptr++;
- }
- }
-
- logMessage("%d kB are available", total);
-
- return total;
- }
-
- static int installMethodWindow(struct installMethod ** method) {
- newtComponent form, listbox, okay, text;
- struct installMethod * newMethod;
- int i;
-
- newtOpenWindow(21, 4, 38, 15, "Installation Method");
-
- form = newtForm(NULL, NULL, 0);
-
- text = newtTextbox(3, 1, 32, 2, NEWT_TEXTBOX_WRAP);
- newtTextboxSetText(text, "What type of media contains the packages "
- "to be installed?");
-
- listbox = newtListbox(12, 4, 0, NEWT_LISTBOX_RETURNEXIT);
-
- for (i = 0; i < numMethods; i++) {
- newtListboxAddEntry(listbox, methods[i].name, methods + i);
- }
-
- okay = newtButton(14, 11, "Ok");
-
- newtFormAddComponents(form, text, listbox, okay, NULL);
-
- newtRunForm(form);
-
- newMethod = newtListboxGetCurrent(listbox);
-
- newtFormDestroy(form);
- newtPopWindow();
-
- *method = newMethod;
-
- return 0;
- }
-
- int chooseInstallMethod(struct installMethod ** method) {
- int rc;
- do {
- rc = installMethodWindow(method);
- if (rc) return rc;
-
- if ((*method)->prepareImage) {
- rc = (*method)->prepareImage((*method));
- if (rc == INST_ERROR) return rc;
- }
- } while (rc);
-
- return 0;
- }
-
- static int nfsGetSetup(char ** hostptr, char ** dirptr) {
- newtComponent form, okay, cancel, siteEntry, dirEntry, answer, text;
- char * site, * dir;
-
- if (*hostptr) {
- site = *hostptr;
- dir = *dirptr;
- } else {
- site = "";
- dir = "";
- }
-
- newtOpenWindow(15, 4, 50, 14, "NFS Setup");
-
- form = newtForm(NULL, NULL, 0);
- okay = newtButton(10, 10, "Ok");
- cancel = newtButton(30, 10, "Cancel");
-
- text = newtTextbox(1, 1, 47, 5, NEWT_TEXTBOX_WRAP);
- newtTextboxSetText(text,
- "Please enter the following information:\n"
- "\n"
- " o the name or IP number of your NFS server\n"
- " o the directory on that server containing\n"
- " Red Hat Linux for your architecture");
-
- newtFormAddComponent(form, newtLabel(3, 7, "NFS server name :"));
- newtFormAddComponent(form, newtLabel(3, 8, "Red Hat directory:"));
-
- siteEntry = newtEntry(22, 7, site, 24, &site, NEWT_ENTRY_SCROLL);
- dirEntry = newtEntry(22, 8, dir, 24, &dir, NEWT_ENTRY_SCROLL);
-
- newtFormAddComponents(form, text, siteEntry, dirEntry, okay, cancel, NULL);
-
- answer = newtRunForm(form);
- if (answer == cancel) {
- newtFormDestroy(form);
- newtPopWindow();
-
- return INST_CANCEL;
- }
-
- *hostptr = strdup(site);
- *dirptr = strdup(dir);
-
- newtFormDestroy(form);
- newtPopWindow();
-
- return 0;
- }
-
- static int cdromPrepare(struct installMethod * method) {
- char * cddev;
- struct driversLoaded * dl = NULL;
- static int moduleLoaded = 0;
- int rc;
-
- messageWindow("Note", "Insert your Red Hat CD into your CD drive now");
-
- while (1) {
- rc = setupCDdevice(&cddev, &dl);
- if (rc) return rc;
-
- #ifdef __i386__
- if (!moduleLoaded) {
- rc = loadModule("isofs", DRIVER_FS, DRIVER_MINOR_NONE, &dl);
- if (rc) return rc;
- moduleLoaded = 1;
- }
- #endif
-
- rc = doMount(cddev, "/tmp/rhimage", "iso9660", 1, 0);
- if (rc) {
- removeCDmodule(&dl);
- messageWindow("Error",
- "I could not mount a CD on device /dev/%s", cddev);
- continue;
- }
-
- if (access("/tmp/rhimage/RedHat", R_OK)) {
- umount("/tmp/rhimage");
- removeCDmodule(&dl);
- messageWindow("Error", "That CDROM device does not seem "
- "to contain a Red Hat CDROM.");
- continue;
- }
-
- break;
- }
-
- if (!access("/tmp/rhimage/RedHat/instimage/lib", X_OK)) {
- unlink("/tmp/rhimage/RedHat/instimage/lib");
- symlink("/tmp/rhimage/RedHat/instimage/lib", "/lib");
- }
-
- if (!access("/tmp/rhimage/RedHat/instimage/usr/bin", X_OK)) {
- unlink("/tmp/rhimage/RedHat/instimage/usr/bin");
- symlink("/tmp/rhimage/RedHat/instimage/usr/bin", "/usr/bin");
- }
-
- writeModuleConf("/tmp", dl, 0);
-
- return 0;
- }
-
- static int nfsPrepare(struct installMethod * method) {
- struct netInterface intf;
- struct netConfig netc;
- struct driversLoaded * dl = NULL;
- char * host = NULL, * dir = NULL;
- char * buf;
- static int moduleLoaded = 0;
- enum { NFS_STEP_NET, NFS_STEP_INFO, NFS_STEP_MOUNT, NFS_STEP_DONE }
- step = NFS_STEP_NET;
- int rc;
-
- memset(&intf, 0, sizeof(intf));
- memset(&netc, 0, sizeof(netc));
-
- while (step != NFS_STEP_DONE) {
- switch (step) {
- case NFS_STEP_NET:
- rc = bringUpNetworking(&intf, &netc, &dl);
- if (rc) return rc;
- step = NFS_STEP_INFO;
- break;
-
- case NFS_STEP_INFO:
- rc = nfsGetSetup(&host, &dir);
- if (rc == INST_CANCEL)
- step = NFS_STEP_NET;
- else if (rc == INST_ERROR)
- return INST_ERROR;
- else
- step = NFS_STEP_MOUNT;
- break;
-
- case NFS_STEP_MOUNT:
- if (!strlen(host) || !strlen(dir))
- rc = INST_ERROR;
- else {
- buf = malloc(strlen(host) + strlen(dir) + 10);
- strcpy(buf, host);
- strcat(buf, ":");
- strcat(buf, dir);
-
- #ifdef __i386__
- if (!moduleLoaded) {
- rc = loadModule("nfs", DRIVER_FS, DRIVER_MINOR_NONE, &dl);
- if (rc) return rc;
- moduleLoaded = 1;
- }
- #endif
-
- rc = doMount(buf, "/tmp/rhimage", "nfs", 1, 0);
- free(buf);
- }
-
- if (rc) {
- step = NFS_STEP_INFO;
- messageWindow("Error",
- "I could not mount that directory from the server");
- } else {
- if (access("/tmp/rhimage/RedHat", R_OK)) {
- step = NFS_STEP_INFO;
- messageWindow("Error", "That directory does not seem "
- "to contain a Red Hat installation tree.");
- umount("/tmp/rhimage");
- } else
- step = NFS_STEP_DONE;
- }
-
- break;
-
- case NFS_STEP_DONE:
- break;
- }
- }
-
- free(host);
- free(dir);
-
- writeNetInterfaceConfig("/tmp", &intf);
- writeNetConfig("/tmp", &netc, &intf, 1);
- writeModuleConf("/tmp", dl, 0);
-
- if (!access("/tmp/rhimage/RedHat/instimage/lib", X_OK)) {
- unlink("/tmp/rhimage/RedHat/instimage/lib");
- symlink("/tmp/rhimage/RedHat/instimage/lib", "/lib");
- }
-
- if (!access("/tmp/rhimage/RedHat/instimage/usr/bin", X_OK)) {
- unlink("/tmp/rhimage/RedHat/instimage/usr/bin");
- symlink("/tmp/rhimage/RedHat/instimage/usr/bin", "/usr/bin");
- }
-
- return 0;
- }
-
- int floppyRoot(struct installMethod * method) {
- newtComponent form, text, okay, cancel, answer;
- static int isMounted = 0;
-
- if (isMounted) return 0;
-
- if (access("/usr/bin/runinstall2", R_OK)) {
- newtOpenWindow(20, 4, 40, 15, "Second Floppy");
-
- text = newtTextbox(1, 1, 38, 5, NEWT_TEXTBOX_WRAP);
- newtTextboxSetText(text,
- "This install method requires a second disk. Please remove "
- "the boot disk currently in your drive and replace it with "
- "the Red Hat Supplementary Install disk.");
-
- okay = newtButton(6, 10, "Ok");
- cancel = newtButton(24, 10, "Cancel");
-
- form = newtForm(NULL, NULL, 0);
- newtFormAddComponents(form, text, okay, cancel, NULL);
-
- answer = newtRunForm(form);
-
- newtFormDestroy(form);
- newtPopWindow();
-
- if (answer == cancel) return INST_CANCEL;
-
- if (testing) return 0;
-
- while (doMount("fd0", "/tmp/image", "ext2", 1, 0) ||
- access("/tmp/image/usr/bin/runinstall2", R_OK)) {
- /* in case the mount succeeded */
- umount("/tmp/image");
-
- newtOpenWindow(20, 4, 40, 15, "Second Floppy");
- text = newtTextbox(1, 1, 38, 5, NEWT_TEXTBOX_WRAP);
-
- newtTextboxSetText(text,
- "I failed to mount the floppy. Please insert the "
- "Red Hat Supplementary Install disk, or choose "
- "Cancel to pick a different installation process.");
-
- okay = newtButton(6, 10, "Ok");
- cancel = newtButton(24, 10, "Cancel");
-
- form = newtForm(NULL, NULL, 0);
- newtFormAddComponents(form, text, okay, cancel);
-
- answer = newtRunForm(form);
-
- newtFormDestroy(form);
- newtPopWindow();
-
- if (answer == cancel) return INST_CANCEL;
- }
-
- if (totalMemory() > 8000) {
- umount("/tmp/image");
- loadRamdisk("ram2", "fd0", 1440, "Loading supplemental disk...");
- if (doMount("ram2", "/tmp/image", "ext2", 1, 0)) {
- errorWindow("Error mounting ramdisk. This shouldn't "
- "happen, and I'm rebooting your system now.");
- exit(1);
- }
- }
-
-
- symlink("/tmp/image/lib", "/lib");
- symlink("/tmp/image/etc", "/etc");
- symlink("/tmp/image/usr/bin", "/usr/bin");
- }
-
- isMounted = 1;
-
- return 0;
- }
-
-